package com.c1202m.gui;

import com.c1202m.dao.DangNhapDAO;
import com.c1202m.util.MyTableFilter;
import com.c1202m.model.DanhSachTKTableModel;
import com.c1202m.model.RutTienTableModel;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;
import com.c1202m.model.ChuyenKhoanTableModel;
import com.c1202m.model.GuiTienTableModel;
import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.swingx.JXTableHeader;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterFactory;

public class BangGiaoDich extends javax.swing.JPanel {

    DanhSachTKTableModel danhSachTKModel = new DanhSachTKTableModel();
    RutTienTableModel rutTienModel = new RutTienTableModel();
    GuiTienTableModel guiTienModel = new GuiTienTableModel();
    ChuyenKhoanTableModel chuyenKhoanModel = new ChuyenKhoanTableModel();
    Highlighter simpleStripHL = HighlighterFactory.createSimpleStriping();
    public static String selectedSoTK;
    MyTableFilter filterController = null;

    public BangGiaoDich() {
        initComponents();
        bangDanhSachTK();
        bangRutTien();
        bangGuiTien();
        bangChuyenKhoan();
    }

    private void bangDanhSachTK() {
        jxDanhSach.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxDanhSach.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        jxDanhSach.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (jxDanhSach.getSelectedRow() != -1) {
                    btnRutTien.setEnabled(true);
                    btnGuiTien.setEnabled(true);
                    btnChuyenKhoan.setEnabled(true);
                    jxRutTien.setModel(new RutTienTableModel());
                    jxRutTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());

                    jxGuiTien.setModel(new GuiTienTableModel());
                    jxGuiTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());

                    jxChuyenKhoan.setModel(new ChuyenKhoanTableModel());
                    jxChuyenKhoan.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
                } else {
                    btnRutTien.setEnabled(false);
                    btnGuiTien.setEnabled(false);
                    btnChuyenKhoan.setEnabled(false);
                }
                if (jxDanhSach.getSelectedRow() != -1) {
                    int selected = jxDanhSach.convertRowIndexToModel(jxDanhSach.getSelectedRow());
                    selectedSoTK = (String) jxDanhSach.getModel().getValueAt(selected, 0);
                }
            }
        });

        TableColumnModel columnModel = jxDanhSach.getColumnModel();
        jxDanhSach.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxDanhSach.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
        filterController = new MyTableFilter(jxDanhSach);
        BindingGroup filterGroup = new BindingGroup();
        filterGroup.addBinding(Bindings.createAutoBinding(READ,
                txtSearchDS, BeanProperty.create("text"),
                filterController, BeanProperty.create("filterString")));
        filterGroup.bind();
    }

    private void bangRutTien() {
        jxRutTien.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxRutTien.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxRutTien.getColumnModel();
        jxRutTien.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxRutTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    private void bangGuiTien() {
        jxGuiTien.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxGuiTien.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxGuiTien.getColumnModel();
        jxGuiTien.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxGuiTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    private void bangChuyenKhoan() {
        jxChuyenKhoan.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxChuyenKhoan.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxChuyenKhoan.getColumnModel();
        jxChuyenKhoan.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxChuyenKhoan.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    //<editor-fold defaultstate="collapsed" desc="reload table">
    public static void lamMoiBangDanhSachTK() {
        jxDanhSach.setModel(new DanhSachTKTableModel());
        jxDanhSach.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    public static void lamMoiBangRutTien() {
        jxRutTien.setModel(new RutTienTableModel());
        jxRutTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    public static void lamMoiBangGuiTien() {
        jxGuiTien.setModel(new GuiTienTableModel());
        jxGuiTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }

    public static void lamMoiBangChuyenKhoan() {
        jxChuyenKhoan.setModel(new ChuyenKhoanTableModel());
        jxChuyenKhoan.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
    }
    //</editor-fold>

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jxDanhSach = new org.jdesktop.swingx.JXTable();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        jxRutTien = new org.jdesktop.swingx.JXTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jxGuiTien = new org.jdesktop.swingx.JXTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        jxChuyenKhoan = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        txtSearchDS = new org.jdesktop.swingx.JXSearchField();
        btnRutTien = new javax.swing.JButton();
        btnGuiTien = new javax.swing.JButton();
        btnChuyenKhoan = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(760, 430));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tài Khoản", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(102, 0, 0))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(378, 310));

        jxDanhSach.setModel(danhSachTKModel);
        jxDanhSach.setHighlighters(simpleStripHL);
        jxDanhSach.setName("bangDanhSachTK"); // NOI18N
        jxDanhSach.setShowGrid(false);
        jScrollPane1.setViewportView(jxDanhSach);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 5, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Giao Dịch", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(102, 0, 0))); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(378, 310));

        jxRutTien.setModel(rutTienModel);
        jxRutTien.setHighlighters(simpleStripHL);
        jxRutTien.setShowGrid(false);
        jScrollPane3.setViewportView(jxRutTien);

        jTabbedPane1.addTab("Rút Tiền", jScrollPane3);

        jxGuiTien.setModel(guiTienModel);
        jxGuiTien.setHighlighters(simpleStripHL);
        jxGuiTien.setShowGrid(false);
        jScrollPane4.setViewportView(jxGuiTien);

        jTabbedPane1.addTab("Gửi Tiền", jScrollPane4);

        jxChuyenKhoan.setModel(chuyenKhoanModel);
        jxChuyenKhoan.setHighlighters(simpleStripHL);
        jxChuyenKhoan.setShowGrid(false);
        jScrollPane5.setViewportView(jxChuyenKhoan);

        jTabbedPane1.addTab("Chuyển Khoản", jScrollPane5);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setText("Tìm kiếm theo số tài khoản hoặc chủ tài khoản");
        jLabel1.setPreferredSize(new java.awt.Dimension(220, 15));

        txtSearchDS.setToolTipText("Nhập từ khóa tìm kiếm");
        txtSearchDS.setPreferredSize(new java.awt.Dimension(220, 20));
        txtSearchDS.setPrompt("Từ khóa tìm kiếm");

        btnRutTien.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-withdraw.png"))); // NOI18N
        btnRutTien.setText("Rút Tiền");
        btnRutTien.setEnabled(false);
        btnRutTien.setPreferredSize(new java.awt.Dimension(100, 25));
        btnRutTien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRutTienActionPerformed(evt);
            }
        });

        btnGuiTien.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/deposit_icon.gif"))); // NOI18N
        btnGuiTien.setText("Gửi Tiền");
        btnGuiTien.setEnabled(false);
        btnGuiTien.setPreferredSize(new java.awt.Dimension(100, 25));
        btnGuiTien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuiTienActionPerformed(evt);
            }
        });

        btnChuyenKhoan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/chuyenkhoan.png"))); // NOI18N
        btnChuyenKhoan.setText("Chuyển Khoản");
        btnChuyenKhoan.setEnabled(false);
        btnChuyenKhoan.setPreferredSize(new java.awt.Dimension(130, 25));
        btnChuyenKhoan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChuyenKhoanActionPerformed(evt);
            }
        });

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        btnRefresh.setText("Làm mới");
        btnRefresh.setMargin(new java.awt.Insets(2, 8, 2, 8));
        btnRefresh.setPreferredSize(new java.awt.Dimension(80, 25));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSearchDS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(btnRutTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(btnGuiTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(btnChuyenKhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(77, 77, 77))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(txtSearchDS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRutTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuiTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChuyenKhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRutTienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRutTienActionPerformed
        int selected = jxDanhSach.convertRowIndexToModel(jxDanhSach.getSelectedRow());
        String selectedSoTK = (String) jxDanhSach.getModel().getValueAt(selected, 0);
        new RutTien(DangNhapDAO.as, false, selectedSoTK);
    }//GEN-LAST:event_btnRutTienActionPerformed

    private void btnGuiTienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuiTienActionPerformed
        int selected = jxDanhSach.convertRowIndexToModel(jxDanhSach.getSelectedRow());
        String selectedSoTK = (String) jxDanhSach.getModel().getValueAt(selected, 0);
        new GuiTien(DangNhapDAO.as, false, selectedSoTK);
    }//GEN-LAST:event_btnGuiTienActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        lamMoiBangDanhSachTK();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnChuyenKhoanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChuyenKhoanActionPerformed
        int selected = jxDanhSach.convertRowIndexToModel(jxDanhSach.getSelectedRow());
        String selectedSoTK = (String) jxDanhSach.getModel().getValueAt(selected, 0);
        new ChuyenKhoan(DangNhapDAO.as, false, selectedSoTK);
    }//GEN-LAST:event_btnChuyenKhoanActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChuyenKhoan;
    private javax.swing.JButton btnGuiTien;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnRutTien;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    public static org.jdesktop.swingx.JXTable jxChuyenKhoan;
    public static org.jdesktop.swingx.JXTable jxDanhSach;
    public static org.jdesktop.swingx.JXTable jxGuiTien;
    public static org.jdesktop.swingx.JXTable jxRutTien;
    private org.jdesktop.swingx.JXSearchField txtSearchDS;
    // End of variables declaration//GEN-END:variables
}
